#"Follow the Leaders" JOP replication
#July 2024

#set wd
setwd("~/Documents/JOP RnR")

#load packages
library(dplyr)
library(MASS)
library(tidyverse)
library(ggplot2)
library(ggpubr)
library(hrbrthemes)
library(stargazer)

#upload data
data <- read.csv("Express Yourself_Data for JOP.csv")
head(data)

#Fig 2, Member Distribution of Vote Ideal Points, 115th- 116th Congress
voteplot<- ggplot(data, aes(x=std_vote_ip, fill=party)) +
  geom_histogram(position="identity", alpha=0.6, bins=110)+
  scale_color_manual(values=c("darkblue", "brown"))+
  scale_fill_manual(values=c("darkblue", "brown"), name="Party")+ xlab("Vote Ideal Point") + ylab("Member Count")+
  xlim(c(-3, 3))

speechplot <- ggplot(data, aes(x=std_speech_ip, fill=party)) +
  geom_histogram(position="identity", alpha=0.6, bins=110)+
  scale_color_manual(values=c("darkblue", "brown"))+
  scale_fill_manual(values=c("darkblue", "brown"), name="Party")+ xlab("Speech Ideal Point") + ylab("Member Count") +
  xlim(c(-3, 3))

tweetplot<- ggplot(data, aes(x=std_tweet_ip, fill=party)) +
  geom_histogram(position="identity", alpha=0.6, bins=110)+
  scale_color_manual(values=c("darkblue", "brown"))+
  scale_fill_manual(values=c("darkblue", "brown"), name="Party")+ xlab("Tweet Ideal Point") + ylab("Member Count") +
  xlim(c(-3, 3))

ggarrange(voteplot, speechplot, tweetplot, 
          ncol=3, nrow=1,
          common.legend = T, legend="right")

ggsave("Fig2.jpg")

##Table A3: Categorical Changes in Ranked Ideal Point Position, 115th – 116th Congress, Interactive Models
data<-data %>%
  mutate(partylead = ifelse(goplead==1 |
                              demlead==1, 1,0))
head(data)
         
TableA3_votespeech_interaction <- polr(as.factor(VS_gapcat_invert )~party*female+ party*nonwhite+
                                         party*number_of_house_terms+party*rlvthouse + party*trump16 + 
                                         party*suburban + party*urban + party*district_percent_white +
                                         party*district_unemployment_rate + party*probsolve + 
                                         rsc+freedom +prog+newdems+bluedog+
                                         partylead*party+party*cmtechair+party*topcmte, 
                                       data=data)

TableA3_votetweet_interaction <- polr(as.factor(VT_gapcat_invert)~party*female+ party*nonwhite+
                                        party*number_of_house_terms+party*rlvthouse + party*trump16 + 
                                        party*suburban + party*urban + party*district_percent_white +
                                        party*district_unemployment_rate +  party*probsolve + 
                                        rsc+freedom +prog+newdems+bluedog+
                                        partylead*party+party*cmtechair+party*topcmte, 
                                      data=data)

#create table output
stargazer(TableA3_votespeech_interaction, TableA3_votetweet_interaction, type="html",
          dep.var.labels = c("Shift Vote to Speech", "Shift Vote to Tweet"),
          covariate.labels = c("Republican MC", "Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem solvers caucus", 
                               "Republican study group (Rep)", "Freedom Caucus (Rep)",
                               "Progressive Caucus (Dem)", "New Democratic Coalition (Dem)",
                               "Blue Dog Coalition (Dem)", "Party leader", "Committee chair",
                               "Top committee member", "Republican * Female MC",
                               "Republican*Non-White MC", "Republican*Number of terms served",
                               "Republican*Percent vote in last election", "Republican*District vote for Trump",
                               "Republican*Suburban district", "Republican*Urban district", "Republican*District percent white",
                               "Republican*District unemployment", "Republican*Problem Solvers Caucus",
                               "Republican*Party leader", "Republican*Committee chair", "Republican*top committee member", "Constant"),
          out="TableA3.html")

#Table A4: Democratic Party Ideal Points Across Venues, 115th – 116th Congress
#divide data by party
data_gop <- subset(data, party == "Republican")
data_dem <- subset(data, party == "Democrat")

TableA4_demvote <- glm(std_vote_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                         trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                         probsolve + prog +
                         bluedog + partylead  + cmtechair + 
                         topcmte, data=data_dem)

TableA4_demspeech <- glm(std_speech_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                           trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                           probsolve + prog + 
                           bluedog + partylead  + cmtechair + 
                           topcmte, data=data_dem)

TableA4_demtwet <- glm(std_tweet_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                         trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                         probsolve +  prog + 
                         bluedog + partylead  + cmtechair + 
                         topcmte, data=data_dem)

stargazer(TableA4_demvote, TableA4_demspeech, TableA4_demtwet, type="html",
          dep.var.labels = c("Vote Ideal Point", "Speech Ideal Point", "Tweet Ideal Point"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem solvers caucus",
                               "Progressive Caucus", "New Democratic Coalition",
                               "Blue Dog Coalition", "Party leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA4.html")

#Table A5: Republican Party Ideal Points Across Venues, 115th – 116th Congress
TableA5_gopvote <- glm(std_vote_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                         trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                         probsolve + rsc + freedom + partylead + cmtechair + 
                         topcmte, data=data_gop)

TableA5_gopspeech <- glm(std_speech_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                           trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                           probsolve + rsc + freedom + partylead + cmtechair + 
                           topcmte, data=data_gop)

TableA5_goptweet <- glm(std_tweet_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                          trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                          probsolve + rsc + freedom + partylead + cmtechair + 
                          topcmte, data=data_gop)
summary(TableA5_goptweet)
stargazer(TableA5_gopvote, TableA5_gopspeech, TableA5_goptweet, type="html",
          dep.var.labels = c("Vote Ideal Point", "Speech Ideal Point", "Tweet Ideal Point"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Republican Study Committee", "Freedom Caucus",
                               "Party leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA5.html")

#Table A6: Full Congressional OLS Model, TBIP
TableA6_congvote <- glm(std_vote_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                          trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                          probsolve + rsc + freedom + prog + 
                          bluedog + goplead+ demlead + cmtechair + 
                          topcmte, data=data)

TableA6_congspeech <- glm(std_speech_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                            trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                            probsolve + rsc + freedom + probsolve + prog + 
                            bluedog + demlead+ goplead  + cmtechair+
                            topcmte, data=data)

TableA6_congtweet <- glm(std_tweet_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                           trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                           probsolve + rsc + freedom + probsolve + prog + 
                           bluedog + demlead+ goplead  + cmtechair+
                           topcmte, data=data)
summary(TableA6_congspeech)
stargazer(TableA6_congvote, TableA6_congspeech, TableA6_congtweet, type="html",
          dep.var.labels = c("Vote", "Speech", "Tweet"),
          covariate.labels = c("Female MC","Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus", 
                               "Republican Study Group (Rep)", "Freedom Caucus (Rep)",
                               "Progressive Caucus (Dem)", "Blue Dog Coalition (Dem)", 
                               "Dem Leader", "GOP Leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA6.html")

#Tables A7-A12: Comparison of standardized ideal points and non-standardized ideal points, 
##by party and ideal point venue

#Table A7: Republican vote scores, Non-standardized TBIP
data2 <- read.csv("data_FTL_JOP.csv")
data2_gop <- subset(data2, party == "Republican")
data2_dem <- subset(data2, party == "Democrat")
TableA7_rawgopvote <- glm(TBIP_Votes ~ female + nonwhite + number_of_house_terms + rlvthouse +
                            trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                            probsolve + rsc + freedom + goplead + cmtechair + 
                            topcmte, data=data2_gop)

TableA7_stdvote <- glm(std_vote_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                            trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                            probsolve + rsc + freedom + goplead + cmtechair + 
                            topcmte, data=data_gop)

stargazer(TableA7_rawgopvote, TableA7_stdvote, type="html",
          dep.var.labels = c("Non-Standardized TBIP, votes", "Standardized TBIP, votes"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Republican Study Committee", "Freedom Caucus",
                               "GOP leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA7.html")

#Table A8: Democratic vote scores, Non-standardized TBIP
TableA8_rawdemvote <- glm(TBIP_Votes ~ female + nonwhite + number_of_house_terms + rlvthouse +
                            trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                            probsolve + prog +
                            bluedog + demlead  + cmtechair + 
                            topcmte, data=data2_dem)

TableA8_stddemvote <- glm(std_vote_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                            trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                            probsolve + prog +
                            bluedog + demlead  + cmtechair + 
                            topcmte, data=data2_dem)

stargazer(TableA8_rawdemvote, TableA8_stddemvote, type="html",
          dep.var.labels = c("Non-Standardized TBIP, votes", "Standardized TBIP, votes"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Progressive Caucus", "Blue Dog Democrats",
                               "Dem leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA8.html")

#Table A9: Republican speech scores, Non-standardized TBIP 
TableA9_rawgopspeech <- glm(tbip_floor_speeches ~ female + nonwhite + number_of_house_terms + rlvthouse +
                              trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                              probsolve + rsc + freedom + goplead + cmtechair + 
                              topcmte, data=data2_gop)

TableA9_stdgopspeech <- glm(std_speech_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                              trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                              probsolve + rsc + freedom + goplead + cmtechair + 
                              topcmte, data=data2_gop)

stargazer(TableA9_rawgopspeech, TableA9_stdgopspeech, type="html",
          dep.var.labels = c("Non-Standardized TBIP, speech", "Standardized TBIP, speech"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Republican Study Committee", "Freedom Caucus",
                               "GOP leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA9.html")

#Table A10: Democratic speech scores, Non-standardized TBIP 
TableA10_rawdemspeech <- glm(tbip_floor_speeches ~ female + nonwhite + number_of_house_terms + rlvthouse +
                               trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                               probsolve + prog +
                               bluedog + demlead  + cmtechair + 
                               topcmte, data=data2_dem)

TableA10_stddemspeech <- glm(std_speech_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                               trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                               probsolve + prog +
                               bluedog + demlead  + cmtechair + 
                               topcmte, data=data_dem)

stargazer(TableA10_rawdemspeech, TableA10_stddemspeech, type="html",
          dep.var.labels = c("Non-Standardized TBIP, votes", "Standardized TBIP, votes"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Progressive Caucus", "Blue Dog Democrats",
                               "Dem leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA10.html")

#Table A11: Republican tweet scores, Non-standardized TBIP 
TableA11_rawgoptweet <- glm(tbip_tweets ~ female + nonwhite + number_of_house_terms + rlvthouse +
                              trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                              probsolve + rsc + freedom + goplead + cmtechair + 
                              topcmte, data=data2_gop)

TableA11_stdgoptweet <- glm(std_tweet_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                              trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                              probsolve + rsc + freedom + goplead + cmtechair + 
                              topcmte, data=data2_gop)

stargazer(TableA11_rawgoptweet, TableA11_stdgoptweet, type="html",
          dep.var.labels = c("Non-Standardized TBIP, tweet", "Standardized TBIP, tweet"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Republican Study Committee", "Freedom Caucus",
                               "GOP leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA11.html")

#Table A12: Democratic tweet scores, Non-standardized TBIP 
TableA12_rawdemtweet <- glm(tbip_tweets ~ female + nonwhite + number_of_house_terms + rlvthouse +
                              trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                              probsolve + prog +
                              bluedog + demlead  + cmtechair + 
                              topcmte, data=data2_dem)

TableA12_stddemtweet <- glm(std_tweet_ip ~ female + nonwhite + number_of_house_terms + rlvthouse +
                              trump16 + suburban + urban + district_percent_white + district_unemployment_rate +
                              probsolve + prog +
                              bluedog + demlead  + cmtechair + 
                              topcmte, data=data2_dem)

stargazer(TableA12_rawdemtweet, TableA12_stddemtweet, type="html",
          dep.var.labels = c("Non-Standardized TBIP, tweet", "Standardized TBIP, tweet"),
          covariate.labels = c("Female MC", "Non-White MC", "Number of terms served", 
                               "Percent vote in last election", "District vote for Trump",
                               "Suburban district", "Urban district", "District percent white",
                               "District unemployment rate", "Problem Solvers Caucus",
                               "Progressive Caucus", "Blue Dog Democrats",
                               "Dem leader", "Committee chair",
                               "Top committee member", "Constant"),
          out="TableA12.html")

#Figure A1: Individual Member Ideal Points, Speech versus Votes
ggplot(data, aes(x = std_vote_ip, y = std_speech_ip, color=party)) +
  geom_point()+
  scale_color_manual(values=c('darkblue', 'brown'), name="Party")+
  xlab("Vote-based Ideal Point")+
  ylab("TBIP, Floor Speeches")+
  xlim(c(-2, 2))+
  ylim(c(-2, 2))
ggsave("FigA1.jpg")

#Figure A2: Individual Member Ideal Points, Tweets versus Votes
ggplot(data, aes(x = std_vote_ip, y = std_tweet_ip, color=party)) +
  geom_point()+
  scale_color_manual(values=c('darkblue', 'brown'), name="Party")+
  xlab("Vote-based Ideal Point")+
  ylab("TBIP, Tweets")+
  xlim(c(-2, 2))+
  ylim(c(-2, 2))
ggsave("FigA2.jpg")

#Figure A3: Individual Member Ideal Points, Tweets versus Speech
ggplot(data, aes(x = std_speech_ip, y = std_vote_ip, color=party)) +
  geom_point()+
  scale_color_manual(values=c('darkblue','brown'), name="Party")+
  xlab("TBIP, Speech")+
  ylab("TBIP, Tweets")+
  xlim(c(-2, 2))+
  ylim(c(-2, 2))
ggsave("FigA3.jpg")

#Figure A4: Independent-level shifts, vote IP to speech IP
ggplot(data, aes(x=VS_gap , color=party, fill=party)) + 
  geom_histogram(binwidth=3) +
  scale_fill_manual(values=c('darkblue', 'brown'), name="Party")+
  scale_color_manual(values=c('darkblue', 'brown'), name="Party")+
  ggtitle("Vote-Speech Ranking")+
  xlim(-400, 400)+
  xlab("Change in ranking, Vote-Speech")+
  ylab("")
ggsave("FigA4.jpg")

#Figure A5: Independent-level shifts, vote IP to Tweet IP
ggplot(data, aes(x=VT_gap, color=party, fill=party)) + 
  geom_histogram(binwidth=3) +
  scale_fill_manual(values=c('darkblue', 'brown'), name="Party")+
  scale_color_manual(values=c('darkblue', 'brown'), name="Party")+
  ggtitle("Vote-Tweet Ranking")+
  xlim(-400, 400)+
  xlab("Change in ranking, Vote-Tweet")+
  ylab("")
ggsave("FigA5.jpg")

